remotemanager.storage.sendablemixin module¶
This module handles the base functionality for serialising and unserialising objects.
The SendableMixin class provides the necessary methods to convert objects to YAML format and vice versa.
- class remotemanager.storage.sendablemixin.SendableMixin[source]¶
Mixin class to create “sendable” object. Provides methods for conversion to yaml format
Create a sendable object by subclassing SendableMixin at class creation
>>> class MyObject(SendableMixin): >>> ...
Instances of this object will now have the required methods to be converted to and from dict format
>>> new = MyObject() >>> payload = new.pack() # store the object in a dict object >>> recreated = MyObject.unpack(payload) # create an instance from dict
- pack(uuid: str = None, file: str = None) dict | None [source]¶
“packs” the object into a dict-format, ready for yaml-dump
- Parameters:
uuid (str) –
Optional uuid string to package this data inside. Adds a toplevel uuid to the payload dict: >>> p = {…}
Becomes: >>> p = {uuid: {…}}
file (str) – Directly package to file with yaml.dump
- Returns:
object payload
- Return type:
(dict)
- classmethod unpack(data: dict = None, file: str = None, **kwargs)[source]¶
Re-create an object from a packaged payload coming from
obj.pack
Note
use this function to unpack from a payload _outside_ an object
newobj = MyObject.unpack(payload)
Where
MyObject
is a subclass of SendableMixin, andpayload
is a dict-type coming fromMyObject.pack()
- Parameters:
data (dict) – __dict__ payload from the object that was packaged
file (str) – filepath to unpack from, if data is not given
limit (bool) – set False to allow outside classes to be unserialised
- Returns:
re-created object
- inject_payload(payload: dict)[source]¶
inject payload into the __dict__, effectively re-creating the object
Note
use this function to unpack _within_ an object
>>> class MyObject(SendableMixin): >>> def __init__(self, ...): >>> ... >>> self.inject_payload(payload)
- Parameters:
payload (dict) – __dict__ payload from the object that was packaged
- serialise(obj)[source]¶
Recurse over any iterable objects, or call the pack() method of any SendableMixin objects, for serialisation
- Parameters:
obj – object to be packaged
- Returns (yaml-serialisable object):
yaml-friendly object
- remotemanager.storage.sendablemixin.get_class_storage(obj) Dict[str, str] [source]¶
Breaks down object into its module and classname.
- Parameters:
obj – Python object to be broken down
- Returns (dict):
module and classname dict
- remotemanager.storage.sendablemixin.get_mro_classnames(obj) List[str] [source]¶
Retrieves a list of class names from the Method Resolution Order (MRO) of an object.
- Parameters:
obj – Python object whose MRO is to be retrieved.
- Returns:
A list containing the class names in the order they appear in the MRO.
- Return type:
List[str]